package com.hd.persistence.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hd.common.DataPage;
import com.hd.common.HibernateEntityDao;
import com.hd.persistence.model.Person;

public class PersonDao extends HibernateEntityDao<Person> {
	
	public DataPage<Person> pageQueryPersons(int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder("from Person ");
		return pagedQuery(hql.toString(), pageNo, pageSize);
	}
	
	public DataPage<Person> pageQueryPersons(String name, Date beginTime, int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder("from Person where (1=1) ");
		List<Object> values = new ArrayList<Object>();
		if ( StringUtils.isNotBlank(name) ) {
			values.add(name);
			hql.append("and name=? ");
		}
		if ( beginTime != null ) {
			values.add(beginTime);
			hql.append("and beginTime=? ");
		}
		return pagedQuery(hql.toString(), pageNo, pageSize, values.toArray());
	}
	
	public Person findPersonBy(String loginId, String password) {
		if ( StringUtils.isNotBlank(loginId) && StringUtils.isNotBlank(password) ) {
			String hql = new String("from Person where loginId=? and password=? ");
			List<Person> persons = find(hql, loginId, password);
			if ( persons.size() > 0 ) 
				return persons.get(0);
		}
		return null;
	}
	
	public List<Person> listPerson(int category,boolean isAdmin){
		String hql = "";
		if(category == -1){
			hql = new String("from Person where isAdmin="+isAdmin);
		}else{
			hql = new String("from Person where category="+category +"and isAdmin="+isAdmin);
		}
		List<Person> persons = find(hql);

		return persons;
	}

}
